﻿using CCWin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TelephoneYiShou
{
    public partial class FrmSaleCount : Skin_Mac
    {
        public FrmSaleCount()
        {
            InitializeComponent();
        }
        public DataTable dt;
        int i, start;//i表示总行数，start表示当前位置
        int size = 10;//size表示一页显示的数量
        string str1 = "select orderid,c.CommodityName cn,c.colour '颜色',cl.ClientName cln,number,ordertime,paytype,Moneysum,Remark,e.EmployeeName en from SellGoods s,Commodity c ,Client cl ,Employee e where c.CommodityID=s.CommodityID and cl.ClientID = s.ClientID and e.EmployeeID = s.Employeeid";
        string str = "server=.;database = TelephoneYiShou;uid = sa;pwd = 123456";
        private void FrmSaleCount_Load(object sender, EventArgs e)
        {
            dt = DBHelper.GetDataTable(str1);
            i = dt.Rows.Count;//获取数据的条数
            show(0, size);
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据",start+1, i / size + 1, i);//显示数据

        }
        //首页
        private void homepage_Click(object sender, EventArgs e)
        {
            show(0, size);//调用show函数
            start = 0;
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start+1, i / size + 1, i);//显示数据
        }
        //上一页
        private void UpPgae_Click(object sender, EventArgs e)
        {
            if (start < 1)
            {
                MessageBoxEx.Show("已是第一页......");
            }
            else
            {
                start = start - 1;
                show(start * size, size);
            }
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start+1, i / size + 1, i);//显示数据
        }
        //下一页
        private void NextPage_Click(object sender, EventArgs e)
        {   
            if (start+1 > i / size)
            {
                MessageBoxEx.Show("已是最后页");
            }
            else
            {
                start = start+1;
                show(start * size, size);
            }
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start+1, i / size+1, i);//显示数据
        }
        //尾页
        private void TrailerPage_Click(object sender, EventArgs e)
        {
            show(i / size, i - (i / size) * size);
            start = i / size;
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start+1, i / size + 1, i);//显示数据
        }
        //退出页面
        private void buttQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //数量统计
        private void CountSum_Click(object sender, EventArgs e)
        {
            int count = dt.Rows.Count;
            MessageBoxEx.Show("订单数为：" + count);
            
        }
        //金额，售出手机台数统计
        private void buttmoney_Click(object sender, EventArgs e)
        {
            int sum = 0;
            double moneysum = 0;
            for (int i = 0; i < dt.Rows.Count; i++)//循环所有列数
            {
                sum += int.Parse(dt.Rows[i]["number"].ToString());//累加所有数量
                moneysum += double.Parse(dt.Rows[i][7].ToString());
            }
            MessageBoxEx.Show("一共卖出手机"+sum+"台\n"+"收入金额为"+moneysum+"元");
        }
        //开始时间
        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            if(Convert.ToDateTime(dateTimestart.Text) >= Convert.ToDateTime(dateTimeend.Text))
            {
                MessageBoxEx.Show("时间错误");
            }
            else
            {
                str1 = string.Format("select orderid,c.CommodityName cn,c.colour '颜色',cl.ClientName cln,number,ordertime,paytype,Moneysum,Remark,e.EmployeeName en from SellGoods s,Commodity c ,Client cl ,Employee e where c.CommodityID=s.CommodityID and cl.ClientID = s.ClientID and e.EmployeeID = s.Employeeid and ordertime>='{0}' and ordertime<='{1}'", Convert.ToDateTime(dateTimestart.Text),Convert.ToDateTime(dateTimeend.Text));
                dt = DBHelper.GetDataTable(str1);
                i = dt.Rows.Count;//获取数据的条数
                show(0, size);
                labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
            }
        }
        //结束时间
        private void dateTimeend_ValueChanged(object sender, EventArgs e)
        {
            if (Convert.ToDateTime(dateTimestart.Text) >= Convert.ToDateTime(dateTimeend.Text))
            {
                MessageBoxEx.Show("时间错误");
            }
            else
            {
                str1 = string.Format("select orderid,c.CommodityName cn,c.colour '颜色',cl.ClientName cln,number,ordertime,paytype,Moneysum,Remark,e.EmployeeName en from SellGoods s,Commodity c ,Client cl ,Employee e where c.CommodityID=s.CommodityID and cl.ClientID = s.ClientID and e.EmployeeID = s.Employeeid and ordertime>='{0}' and ordertime<='{1}'", Convert.ToDateTime(dateTimestart.Text), Convert.ToDateTime(dateTimeend.Text));
                dt = DBHelper.GetDataTable(str1);
                 
                show(0, size);
                labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
            }

        }
        //显示全部数据
        private void button1_Click(object sender, EventArgs e)
        {
            str1 = "select orderid,c.CommodityName cn,c.colour '颜色',cl.ClientName cln,number,ordertime,paytype,Moneysum,Remark,e.EmployeeName en from SellGoods s,Commodity c ,Client cl ,Employee e where c.CommodityID=s.CommodityID and cl.ClientID = s.ClientID and e.EmployeeID = s.Employeeid";
            dt = DBHelper.GetDataTable(str1);
            i = dt.Rows.Count;//获取数据的条数
            show(0, size);
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
        }
        //查找客户
        private void buttselect_Click(object sender, EventArgs e)
        {
            start = 0;//初始化页数
            string gonshang = textgs.Text;
            if (string.IsNullOrEmpty(gonshang))
            {
                MessageBoxEx.Show("请输入要搜索的内容");
                return;
            }
            //按客户姓名
            if (comanxuanxiang.Text=="客户姓名")
            {
                str1 = string.Format("select orderid,c.CommodityName cn,c.colour '颜色',cl.ClientName cln,number,ordertime,paytype,Moneysum,Remark,e.EmployeeName en from SellGoods s,Commodity c ,Client cl ,Employee e where c.CommodityID=s.CommodityID and cl.ClientID = s.ClientID and e.EmployeeID = s.Employeeid and cl.ClientName like '%{0}%'", gonshang);
                dt = DBHelper.GetDataTable(str1);
                i = dt.Rows.Count;//获取数据的条数
                if (i == 0)
                {
                    MessageBoxEx.Show("没有" + gonshang + "的订单");
                }
                else
                {
                    show(0, size);
                    labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
                }
            }
            //按商品名称
            if (comanxuanxiang.Text == "商品名称")
            {
                str1 = string.Format("select orderid,c.CommodityName cn,c.colour '颜色',cl.ClientName cln,number,ordertime,paytype,Moneysum,Remark,e.EmployeeName en from SellGoods s,Commodity c ,Client cl ,Employee e where c.CommodityID=s.CommodityID and cl.ClientID = s.ClientID and e.EmployeeID = s.Employeeid and c.CommodityName like '%{0}%'", gonshang);
                dt = DBHelper.GetDataTable(str1);
                i = dt.Rows.Count;//获取数据的条数
                if (i == 0)
                {
                    MessageBoxEx.Show("没有" + gonshang + "的订单");
                }
                else
                {
                    show(0, size);
                    labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
                }
            }
            //按支付方式
            if (comanxuanxiang.Text == "支付方式")
            {
                str1 = string.Format("select orderid,c.CommodityName cn,c.colour '颜色',cl.ClientName cln,number,ordertime,paytype,Moneysum,Remark,e.EmployeeName en from SellGoods s,Commodity c ,Client cl ,Employee e where c.CommodityID=s.CommodityID and cl.ClientID = s.ClientID and e.EmployeeID = s.Employeeid and paytype like '%{0}%'", gonshang);
                dt = DBHelper.GetDataTable(str1);
                i = dt.Rows.Count;//获取数据的条数
                if (i == 0)
                {
                    MessageBoxEx.Show("没有" + gonshang + "的订单");
                }
                else
                {
                    show(0, size);
                    labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
                }
            }
            //按操作员
            if (comanxuanxiang.Text == "操作员")
            {
                str1 = string.Format("select orderid,c.CommodityName cn,c.colour '颜色',cl.ClientName cln,number,ordertime,paytype,Moneysum,Remark,e.EmployeeName en from SellGoods s,Commodity c ,Client cl ,Employee e where c.CommodityID=s.CommodityID and cl.ClientID = s.ClientID and e.EmployeeID = s.Employeeid and e.EmployeeName like '%{0}%'", gonshang);
                dt = DBHelper.GetDataTable(str1);
                i = dt.Rows.Count;//获取数据的条数
                if (i == 0)
                {
                    MessageBoxEx.Show("没有" + gonshang + "的订单");
                }
                else
                {
                    show(0, size);
                    labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
                }
            }
        }

        //显示i到j的数据
        public void show(int i,int j)
        {
            SqlConnection sqlcon = new SqlConnection(str);
            sqlcon.Open();
            SqlCommand sqlcmd = new SqlCommand(str1, sqlcon);
            SqlDataAdapter sqlada = new SqlDataAdapter(sqlcmd);
            DataSet ds = new DataSet();
            sqlada.Fill(ds, i, j, "[SellGoods]");//把数据库表中的i-j的记录数用来填充数据集
            dataOrderTable.DataSource = ds.Tables["[SellGoods]"];
            ds = null;//清空数据集
        }
    }
}
